package com.mt.roll.report.controller;


import com.mt.roll.core.base.BaseController;
import com.mt.roll.core.constants.CommonConstant;
import com.mt.roll.core.validator.groups.PageGroup;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.params.ReportStorageBoxQueryParam;
import com.mt.roll.report.service.ReportStorageBoxService;
import com.mt.roll.report.vo.ReportStorageBoxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: yulianchen
 * @Date: 2022/3/4 9:55
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.API_MODULE_REPORT + "report-storage-box")
@Slf4j
@Api(value = "当前库存存储箱报表", tags = "当前库存存储箱报表", hidden = false)
public class ReportStorageBoxController extends BaseController {
    @Autowired
    private ReportStorageBoxService reportStorageBoxService;
    @PostMapping(value = "list")
    @ApiOperation(value = "获取当前库存存储箱列表")
    private R<List<ReportStorageBoxVo>> list(@Validated({Default.class}) @RequestBody ReportStorageBoxQueryParam reportStorageBoxQueryParam) {
        return reportStorageBoxService.list(reportStorageBoxQueryParam);
    }
    @PostMapping(value = "page")
    @ApiOperation(value = "获取当前库存存储箱分页报表")
    private R<PageVo<ReportStorageBoxVo>> page(@Validated({PageGroup.class, Default.class}) @RequestBody ReportStorageBoxQueryParam reportStorageBoxQueryParam) {
        return reportStorageBoxService.page(reportStorageBoxQueryParam);
    }
    @PostMapping("excel-download")
    @ApiOperation(value = "导出当前库存存储箱列表",produces="application/octet-stream")
    public void excelDownload(HttpServletRequest request, HttpServletResponse response, ReportStorageBoxQueryParam reportStorageBoxQueryParam) throws IOException {
        R<List<ReportStorageBoxVo>> rExcelData = reportStorageBoxService.list(reportStorageBoxQueryParam);
//        List<ReportStorageBoxVo> excelData = reportStorageBoxService.page(reportStorageBoxQueryParam).getData().getRecords();
        List<ReportStorageBoxVo> ExcelData = rExcelData.getData();
        //sheetName：工作簿名称
        String sheetName = URLEncoder.encode("存储箱报表", "utf-8");
        //fileName:表名
        String fileName = URLEncoder.encode("StorageBoxReport.xls", "utf-8");
        com.mt.roll.core.utils.ExcelExportUtil.exportXssfExcel(fileName, sheetName, ReportStorageBoxVo.class, ExcelData, request, response);
    }
}
